package com.azdebugit.springb.conman;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class ServerThread implements Runnable {

    private Socket socket = null;

    public ServerThread (Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        String line = null;
        InputStream input;
        OutputStream output;
        String str = System.currentTimeMillis()+"";
        try {
            // 向客户端发送信息
            output = socket.getOutputStream();
            input = socket.getInputStream();
            BufferedReader bff = new BufferedReader(new InputStreamReader(input));
            output.write(str.getBytes("utf-8"));
            output.flush();
            // 半关闭socket
            socket.shutdownOutput();
            // 获取客户端的信息
            while ((line = bff.readLine()) != null) {
                System.out.println(line);
            }

            // 关闭输入输出流
            output.close();
            bff.close();
            input.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start() {
        run();
    }
}